# koroFileHeader at Yungoal acer
# Create: 2021-04-26 10:42:53
# LastEdit: 2021-05-25 16:02:22
"""说明暂无"""
__author__ = '749B'


import unittest
import io
import sys

if __name__ == '__main__':
    import os
    sys.path.append(os.getcwd())
# pylint: disable = import-error
from azure_client.data_format import output_csv


class OutputCsvTestCase(unittest.TestCase):

    def setUp(self) -> None:
        self.stdout = sys.stdout

    def tearDown(self) -> None:
        sys.stdout = self.stdout

    def test_output_csv(self):
        data = []
        out1 = ''
        out2 = ''

        data.append({
            'name': 'Node1', 
            'type': 'microsoft.compute/virtualmachines',
            'location': 'chinaeast'}
        )
        out = 'name,type,location' + '\n'
        out1 += out
        out2 += ',' + out
        out = 'Node1,microsoft.compute/virtualmachines,chinaeast' + '\n'
        out1 += out
        out2 += ',' + out

        data.append({
            'name': 'node2', 
            'type': 'microsoft.compute/virtualmachines',
            'location': 'chinaeast'}
        )
        out = 'node2,microsoft.compute/virtualmachines,chinaeast' + '\n'
        out1 += out
        out2 += ',' + out
        
        data.append({
            'name': 'server3', 
            'type': 'microsoft.compute/virtualmachines',
            'location': 'chinaeast2'}
        )
        out = 'server3,microsoft.compute/virtualmachines,chinaeast2' + '\n'
        out1 += out
        out2 += ',' + out

        data.append({
            'name': 'net_192.168.1.1', 
            'type': 'microsoft.classicnetwork/virtualnetworks',
            'location': 'chinaeast'}
        )
        out = 'net_192.168.1.1,microsoft.classicnetwork/virtualnetworks,chinaeast' + '\n'
        out1 += out
        out2 += ',' + out

        data.append({
            'name': 'web01', 
            'type': 'microsoft.web/sites',
            'location': 'chinaeast'}
        )
        out = 'web01,microsoft.web/sites,chinaeast' + '\n'
        out1 += out
        out2 += ',' + out

        # 输出重定向
        sys.stdout = io.StringIO()
        with self.subTest(comma_begin=False):
            output_csv(data, False)
            self.assertEqual(sys.stdout.getvalue(), out1)
            
        # 之前的输出不要了，重新实例化一个新对象
        sys.stdout = io.StringIO()
        with self.subTest(comma_begin=True):
            output_csv(data, True)
            self.assertEqual(sys.stdout.getvalue(), out2)
        
    def test_modify_column_map(self):
        data = []
        out = ''

        data.append({
            'name': 'Node1', 
            'type': 'microsoft.compute/virtualmachines',
            'location': 'chinaeast'}
        )
        column_map = {
            'name': '名称',
            'type': 'mode',
            'location': "地区",
        }
        out += '名称,mode,地区' + '\n'
        out += 'Node1,microsoft.compute/virtualmachines,chinaeast' + '\n'

        data.append({
            'name': 'node2', 
            'type': 'microsoft.compute/virtualmachines',
            'location': 'chinaeast'}
        )
        out += 'node2,microsoft.compute/virtualmachines,chinaeast' + '\n'
        
        data.append({
            'name': 'server3', 
            'type': 'microsoft.compute/virtualmachines',
            'location': 'chinaeast2'}
        )
        out += 'server3,microsoft.compute/virtualmachines,chinaeast2' + '\n'

        data.append({
            'name': 'net_192.168.1.1', 
            'type': 'microsoft.classicnetwork/virtualnetworks',
            'location': 'chinaeast'}
        )
        out += 'net_192.168.1.1,microsoft.classicnetwork/virtualnetworks,chinaeast' + '\n'

        data.append({
            'name': 'web01', 
            'type': 'microsoft.web/sites',
            'location': 'chinaeast'}
        )
        out += 'web01,microsoft.web/sites,chinaeast' + '\n'

        # 输出重定向
        sys.stdout = io.StringIO()
        with self.subTest():
            output_csv(data, column_map=column_map)
            self.assertEqual(sys.stdout.getvalue(), out)
            
    def test_ignores_column(self):
        data = []
        out = ''

        data.append({
            'name': 'Node1', 
            'type': 'microsoft.compute/virtualmachines',
            'location': 'chinaeast'}
        )
        ignores = ['location']
        out += 'name,type' + '\n'
        out += 'Node1,microsoft.compute/virtualmachines' + '\n'

        data.append({
            'name': 'node2', 
            'type': 'microsoft.compute/virtualmachines',
            'location': 'chinaeast'}
        )
        out += 'node2,microsoft.compute/virtualmachines' + '\n'
        
        data.append({
            'name': 'server3', 
            'type': 'microsoft.compute/virtualmachines',
            'location': 'chinaeast2'}
        )
        out += 'server3,microsoft.compute/virtualmachines' + '\n'

        data.append({
            'name': 'net_192.168.1.1', 
            'type': 'microsoft.classicnetwork/virtualnetworks',
            'location': 'chinaeast'}
        )
        out += 'net_192.168.1.1,microsoft.classicnetwork/virtualnetworks' + '\n'

        data.append({
            'name': 'web01', 
            'type': 'microsoft.web/sites',
            'location': 'chinaeast'}
        )
        out += 'web01,microsoft.web/sites' + '\n'

        # 输出重定向
        sys.stdout = io.StringIO()
        with self.subTest():
            output_csv(data, ignores=ignores)
            self.assertEqual(sys.stdout.getvalue(), out)


if __name__ == '__main__':
    unittest.main()

